package com.usian.controller;

import com.sun.org.apache.regexp.internal.RE;
import com.usian.feign.ContentServiceFeign;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/content/")
public class ContentCategoryController {
    @Autowired
    private ContentServiceFeign contentServiceFeign;

    @RequestMapping("selectContentCategoryByParentId")
    public Result selectContentCategoryByParentId(@RequestParam(defaultValue = "0") Integer id){
        List<TbContentCategory> list =
                contentServiceFeign.selectContentCategoryByParentId(id);
        if (list != null && list.size() > 0) {
            return Result.ok(list);
        }
        return Result.error("查无结果");
    }

    /**
     * 添加内容分类管理
     * @param parentId
     * @param name
     * @return
     */
    @RequestMapping("insertContentCategory")
    public Result insertContentCategory(@RequestParam Integer parentId,@RequestParam String name){
        Integer num=contentServiceFeign.insertContentCategory(parentId,name);
        if (num>0){
            return Result.ok();
        }
        return Result.error("添加失败");
    }

    /**
     *修改内容分类
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("updateContentCategory")
    public Result updateContentCategory(@RequestParam Integer id,@RequestParam String name){
        Integer num=contentServiceFeign.updateContentCategory(id,name);
        if (num>0){
                return Result.ok();
        }
        return Result.error("修改失败");
    }



    @RequestMapping("deleteContentCategoryById")
    public Result deleteContentCategoryById(@RequestParam Integer categoryId){
        Integer num=contentServiceFeign.deleteContentCategoryById(categoryId);
        if (num>0){
            return Result.ok();
        }
        return Result.error("删除失败");
    }


    /**
     * 查询广告先信息
     * @return
     */
    @RequestMapping("/selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(@RequestParam(defaultValue = "1")
                                                         Integer page,@RequestParam(defaultValue = "3") Integer rows,@RequestParam Long categoryId) {
        PageResult pageResult = contentServiceFeign.selectTbContentAllByCategoryId(page, rows, categoryId);
        if (pageResult != null && pageResult.getResult().size() > 0) {
            return Result.ok(pageResult);
        }
        return Result.error("查无结果");
    }

    @RequestMapping("insertTbContent")
    public Result insertTbContent( TbContent tbContent){
        Integer num = contentServiceFeign.insertTbContent(tbContent);
        if (num != null) {
            return Result.ok();
        }
        return Result.error("添加失败");
    }

}
